package com.example.finalsystem.Service;

import com.example.finalsystem.Dao.GoodsMapper;
import com.example.finalsystem.Dao.InListMapper;
import com.example.finalsystem.Dao.ListGoodsMapper;
import com.example.finalsystem.Entity.Deal;
import com.example.finalsystem.Entity.Goods;
import com.example.finalsystem.Entity.InList;
import com.example.finalsystem.Entity.ListGoods;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;

@Service
public class InListService {
    private SqlSession openSession()
    {
        String resource = "mybatis-config.xml";
        InputStream is = InListService.class.getClassLoader().getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        return   sessionFactory.openSession(true);
    }
    public String addInList(String importName,Integer importNumber){
        SqlSession sqlSession =  openSession();
        InList inList = new InList();
        String str =""+(System.currentTimeMillis()/1000);
        inList.setInlistid(str.substring(str.length()-6));
        inList.setImportnumber(importNumber);
        inList.setImportname(importName);
        inList.setState("未确认");
        sqlSession.getMapper(InListMapper.class).insert(inList);
        sqlSession.close();
        return "添加成功";
    }
    public String fixInList(String inListId,String importName,String state,Integer importNumber){
        SqlSession sqlSession = openSession();
        InList inList = sqlSession.getMapper(InListMapper.class).selectByPrimaryKey(inListId);
        if (inList.getState().equals("已确认"))
        {
            return "修改失败,进货单已确认";
        }
        inList.setState(state);
        inList.setImportname(importName);
        inList.setImportnumber(importNumber);
        sqlSession.getMapper(InListMapper.class).updateByPrimaryKey(inList);
        if (state.equals("已确认"))
        {
            Goods goods = sqlSession.getMapper(GoodsMapper.class).selectByPrimaryKey(importName);
            if (goods==null)
            {
                goods = new Goods();
                goods.setGoodsname(importName);
                goods.setGoodsnumber(importNumber);
                goods.setThreshold(20);
                sqlSession.getMapper(GoodsMapper.class).insert(goods);
            }
            else
            {
                goods.setGoodsnumber(importNumber+goods.getGoodsnumber());
                sqlSession.getMapper(GoodsMapper.class).updateByPrimaryKey(goods);
            }
        }
        sqlSession.commit();
        sqlSession.close();
        return "修改成功";
    }
    public String deleteInList(String inListId ){
        SqlSession sqlSession = openSession();
        if (sqlSession.getMapper(InListMapper.class).selectByPrimaryKey(inListId).getState().equals("已确认"))
        {
            return "w无法删除已确认的进货单";
        }
        sqlSession.getMapper(InListMapper.class).deleteByPrimaryKey(inListId);
        sqlSession.commit();
        sqlSession.close();
        return "删除成功";
    }
    public InList getInList(String inListId){
        SqlSession sqlSession =  openSession();
        InList inList =sqlSession.getMapper(InListMapper.class).selectByPrimaryKey(inListId);
        sqlSession.close();
        return inList;
    }
    public List<InList> getAllInList(){
        SqlSession sqlSession =  openSession();
        List<InList> inLists =sqlSession.getMapper(InListMapper.class).getAllInList();
        sqlSession.close();
        return inLists;
    }
}
